package librarianPanels;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.border.LineBorder;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.table.DefaultTableModel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;

import clientGUI.MainGUI;
import clientGUI.PanelDesign;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MessagesPanel extends PanelDesign {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static JTable table;

	/**
	 * Create the panel.
	 */
	public MessagesPanel() {
		setBorder(new LineBorder(new Color(0, 0, 0), 3, true));
		setPreferredSize(new Dimension(644, 462));
		setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(72, 96, 483, 282);
		add(scrollPane);
		
		table = new JTable();
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Date", "Message"
			}
		));
		table.getColumnModel().getColumn(0).setResizable(false);
		table.getColumnModel().getColumn(0).setPreferredWidth(108);
		table.getColumnModel().getColumn(1).setResizable(false);
		table.getColumnModel().getColumn(1).setPreferredWidth(373);
		table.getTableHeader().setReorderingAllowed(false);
		scrollPane.setViewportView(table);
		
		JLabel lblNewLabel = new JLabel("Librarian Messages:");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 29));
		lblNewLabel.setBounds(186, 39, 271, 46);
		add(lblNewLabel);
		
		JButton btnDelete = new JButton("Delete");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				if(table.getSelectedRow()!=-1)
				{
					int n = JOptionPane.showConfirmDialog(new JFrame(),	"Are you sure you want delete this message?","An Inane Question",JOptionPane.YES_NO_OPTION);		
					if(n==1)		
						return;	
					String[] msgToServer=new String[3];
					msgToServer[0]="DeleteMessage";
					msgToServer[1]="SINGLE";
					msgToServer[2]=table.getModel().getValueAt(table.getSelectedRow(),0).toString();
					MainGUI.OBLClient.handleMessageFromClient(msgToServer);
				}
				else 
				{
					JOptionPane.showMessageDialog(new JFrame(),	"ERROR - Nothing selected!","Error",JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnDelete.setBounds(182, 391, 109, 37);
		add(btnDelete);
		
		JButton btnDeleteAll = new JButton("Delete All");
		btnDeleteAll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int n = JOptionPane.showConfirmDialog(new JFrame(),	"Are you sure you want delete all messages?","An Inane Question",JOptionPane.YES_NO_OPTION);		
				if(n==1)		
					return;	
				String[] msgToServer=new String[4];
				msgToServer[0]="DeleteMessage";
				msgToServer[1]="ALL";
				MainGUI.OBLClient.handleMessageFromClient(msgToServer);
			}
		});
		btnDeleteAll.setBounds(343, 391, 109, 37);
		add(btnDeleteAll);
		remove(background);
		add(background);
		String[] msgToServer1=new String[1];
		msgToServer1[0]="GetLibrarianMessages";
		MainGUI.OBLClient.handleMessageFromClient(msgToServer1);
	}
	public static void changeTableContent(String[][] x)
	{
		
		table.setModel(new DefaultTableModel(x,		new String[] {
				"Date", "Message"
			}){
			private static final long serialVersionUID = 1L;
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		});
		table.getTableHeader().setReorderingAllowed(false);
		table.getColumnModel().getColumn(0).setResizable(false);
		table.getColumnModel().getColumn(1).setResizable(false);
		table.getColumnModel().getColumn(0).setPreferredWidth(108);
		table.getColumnModel().getColumn(1).setPreferredWidth(373);
	}

}

